/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerOverview.java
 *
 * Created on 21-nov-2011, 10:32:05
 */
package view;

import java.awt.Color;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Customer;

/**
 *
 * @author Bryan
 */
public class CustomerOverview extends javax.swing.JPanel {
    private boolean selected = false;
    /** Creates new form CustomerOverview */
    public CustomerOverview(boolean selected) {
        initComponents();
        WinkelApplication.mainWindow.setTitle("Klanten overzicht");
        constructTable();
        
        this.selected = selected;
        
        if(selected) {
            btnSelect.setEnabled(true);
            btnOrders.setVisible(false);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollCustomers = new javax.swing.JScrollPane();
        tblCustomers = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        btnBack = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnChange = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();
        btnOrders = new javax.swing.JButton();

        tblCustomers.setAutoCreateRowSorter(true);
        tblCustomers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "klant_Id", "naam", "adres", "postcode", "woonplaats"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCustomers.setName("tblCustomers"); // NOI18N
        scrollCustomers.setViewportView(tblCustomers);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgAdd.gif"))); // NOI18N
        btnAdd.setText("Toevoegen");
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.setPreferredSize(new java.awt.Dimension(89, 23));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        txtSearch.setForeground(new java.awt.Color(204, 204, 204));
        txtSearch.setText("Zoek klant...");
        txtSearch.setToolTipText("Zoek klant op naam.");
        txtSearch.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtSearchFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtSearchFocusLost(evt);
            }
        });
        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.setPreferredSize(new java.awt.Dimension(89, 23));
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgDelete.png"))); // NOI18N
        btnDelete.setText("Verwijderen");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgModify.png"))); // NOI18N
        btnChange.setText("Wijzigen");
        btnChange.setPreferredSize(new java.awt.Dimension(89, 23));
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        btnSelect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgSelect.gif"))); // NOI18N
        btnSelect.setText("Selecteer");
        btnSelect.setEnabled(false);
        btnSelect.setPreferredSize(new java.awt.Dimension(89, 23));
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        btnOrders.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgOrders.png"))); // NOI18N
        btnOrders.setText("Orders");
        btnOrders.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrdersActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollCustomers, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnOrders)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnChange, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollCustomers, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete)
                    .addComponent(btnChange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOrders)
                    .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        WinkelApplication.getInstance().showPanel(new AlterCustomer(false, selected, -1));
    }//GEN-LAST:event_btnAddActionPerformed
    
    // Start code Bryan//End code Bryan
    //back button Vuong
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
    if(selected){    
        WinkelApplication.getInstance().showPanel(new Payment(), 490, 290);
    } else {
        WinkelApplication.getInstance().showPanel(new MainMenu());
    }
}//GEN-LAST:event_btnBackActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
    if(tblCustomers.getSelectedRow() != -1) {
        int delete = Integer.parseInt(tblCustomers.getValueAt(tblCustomers.getSelectedRow(), 0).toString());
        WinkelApplication.getQueryManager().deleteCustomer(delete);
        WinkelApplication.getInstance().showPanel(new CustomerOverview(false));
    } else {
        WinkelApplication.msgbox("Je moet een klant selecteren om te verwijderen!");
    }
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
    if(tblCustomers.getSelectedRow() != -1) {
        int customerID = Integer.parseInt(tblCustomers.getValueAt(tblCustomers.getSelectedRow(), 0).toString());
    
        WinkelApplication.getInstance().showPanel(new AlterCustomer(true, selected, customerID));
    } else {
        WinkelApplication.msgbox("Je moet een klant selecteren om te wijzigen!");
    }
}//GEN-LAST:event_btnChangeActionPerformed

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        String search = txtSearch.getText();
        List<Customer> klanten = WinkelApplication.getQueryManager().searchCustomer(search);
        DefaultTableModel model = (DefaultTableModel) this.tblCustomers.getModel();
        int rowCount = model.getRowCount();
        
        for(int i = 0; i < rowCount; i++) {
            model.removeRow(0);
        }
        
        for(Customer klant: klanten) {
            model.addRow(new Object[] {new Integer(klant.getCustomerId()),
            klant.getName(),
            klant.getAddress(),
            klant.getZipcode(),
            klant.getCity()});
        }
       
       if(txtSearch.getText().isEmpty()) {
           txtSearch.setForeground(Color.gray);
       } else {
           txtSearch.setForeground(Color.black);
       }
    }//GEN-LAST:event_txtSearchKeyReleased

    private void txtSearchFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSearchFocusGained
        if(txtSearch.getText().equalsIgnoreCase("Zoek klant...")) {
            txtSearch.setText("");
        }
    }//GEN-LAST:event_txtSearchFocusGained

    private void txtSearchFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSearchFocusLost
        if(txtSearch.getText().isEmpty()) {
            txtSearch.setText("Zoek klant...");
            txtSearch.setForeground(Color.gray);
        }
    }//GEN-LAST:event_txtSearchFocusLost

private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
    if(tblCustomers.getSelectedRow()>=0){
        int selected = Integer.parseInt(tblCustomers.getValueAt(tblCustomers.getSelectedRow(), 0).toString());
        WinkelApplication.getInstance().showPanel(new Payment(selected), 490, 290);
    } else {
        WinkelApplication.msgbox("U heeft nog geen klant geselecteerd!");
    }
}//GEN-LAST:event_btnSelectActionPerformed

    private void btnOrdersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrdersActionPerformed
        if(tblCustomers.getSelectedRow() != -1) {
            int CustomerID = Integer.parseInt(tblCustomers.getValueAt(tblCustomers.getSelectedRow(), 0).toString());
            WinkelApplication.getInstance().showPanel(new Orders(false, CustomerID));
        } else {
            WinkelApplication.msgbox("Je moet een klant selecteren om de orders ervan te bekijken!");
        }
    }//GEN-LAST:event_btnOrdersActionPerformed
    
    public void constructTable() {
        List<Customer> klanten = WinkelApplication.getQueryManager().getCustomerList();
        DefaultTableModel model = (DefaultTableModel) this.tblCustomers.getModel();
        
        for(Customer klant: klanten) {
            model.addRow(new Object[] {new Integer(klant.getCustomerId()),
            klant.getName(),
            klant.getAddress(),
            klant.getZipcode(),
            klant.getCity()});
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnOrders;
    private javax.swing.JButton btnSelect;
    private javax.swing.JScrollPane scrollCustomers;
    private javax.swing.JTable tblCustomers;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
